<?php
	class Utils{
		public static function parseCurrency($value){
			return preg_replace("/([^0-9\\.])/i", "", $value); 
		}
		public static function formatCurrency($value){
			//$format = array('decimalSeparator'=>',', 'thousandSeparator'=>'-');
			//'numberFormat'=>array('decimals'=>3, ),
			//return Yii::app()->numberFormatter->formatNumber($format);
			return Yii::app()->numberFormatter->format("#,##0",$value);
		}
		
		public static function getPeriodStartDate($period){
			$startPeriod = trim($period)."-"."01";
			return $startPeriod;			
		}
		
		public static function getPeriodEndDate($period){			
			$th = substr($period, 0,4);
			$th = (int) $th;
			$bln = substr($period, 5,2);
			$bln = (int) $bln;
			
			if ($bln == 12){
				$bln=1;
				$th += 1;
			}else{
				$bln += 1;
			}
			
			$bln = "00" .$bln;
			$bln = substr($bln, strlen($bln)-2);
			$tmp = $th ."-".$bln."-01";
			$tmpDate = DateTime::createFromFormat("Y-m-d", $tmp);
			$tmpDate->sub(new DateInterval('P1D'));
			
			return $tmpDate->format("Y-m-d");				
		}
		public static function getNextPeriod($period){
			$th = substr($period, 0,4);
			$th = (int) $th;
			$bln = substr($period, 5,2);
			$bln = (int) $bln;
				
			if ($bln == 12){
				$bln=1;
				$th += 1;
			}else{
				$bln += 1;
			}
				
			$bln = "00" .$bln;
			$bln = substr($bln, strlen($bln)-2);
			return $th."-".$bln;
		}
		public static function getPrevPeriod($period){
			$th = substr($period, 0,4);
			$th = (int) $th;
			$bln = substr($period, 5,2);
			$bln = (int) $bln;
		
			if ($bln == 1){
				$bln=12;
				$th -= 1;
			}else{
				$bln -= 1;
			}
		
			$bln = "00" .$bln;
			$bln = substr($bln, strlen($bln)-2);
			return $th."-".$bln;
		}
		
		public static function getPeriod($ymd = null){
			if ($ymd ==  null){
				return date("Y-m");
			}
			$tmpDate = DateTime::createFromFormat("Y-m-d", $ymd);
			return $tmpDate->format("Y-m");
		}
		
		public static function isValidPeriod($period){
			if (strlen($period) != 7){
				return false;
			}else{
				$th = substr($period, 0,4);
				$th = (int) $th;
				if ($th < 2000 || $th > 3000){
					return false;
				}else{
					$bln = substr($period, 5,2);
					$bln = (int) $bln;
					if ($bln >= 1 and $bln <= 12){
						return true;
					}
				}
			}
			return false;			
		}
		
		
		public static function terbilang($x){
			$abil = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
			if ($x < 12)
				return " " . $abil[$x];
			elseif ($x < 20)
			return Utils::terbilang($x - 10) . "belas";
			elseif ($x < 100)
			return Utils::terbilang($x / 10) . " puluh" . Utils::terbilang($x % 10);
			elseif ($x < 200)
			return " seratus" . Utils::terbilang($x - 100);
			elseif ($x < 1000)
			return Utils::terbilang($x / 100) . " ratus" . Utils::terbilang($x % 100);
			elseif ($x < 2000)
			return " seribu" . Utils::terbilang($x - 1000);
			elseif ($x < 1000000)
			return Utils::terbilang($x / 1000) . " ribu" . Utils::terbilang($x % 1000);
			elseif ($x < 1000000000)
			return Utils::terbilang($x / 1000000) . " juta" . Utils::terbilang($x % 1000000);
		}
	
		public static function activeRecord2Array($activeRecord){
			return array_map(create_function('$m','return $m->getAttributes();'),$activeRecord);
		}
	
		public static function formatDate($strDate,$inFormat = "d-M-Y",$outFormat = "Y-m-d"){
			if ($strDate == "0000-00-00"){
				return "0000-00-00";
			}
			return  date_format(date_create_from_format( $inFormat, $strDate),$outFormat);
		}
	}
?>